#include "Controls/ContainerControl.h"
#include "Utils.h"
#include <memory>
namespace cxgui
{
using namespace std;
void ContainerControl::Update()
{
	Control::Update();
	for(auto control : _controls)
	{
		control->Update();
	}
}

void ContainerControl::DrawControls()
{	
	for(auto control : _controls)
	{
		control->Draw();
	}
}

void ContainerControl::Draw()
{
	DrawBackground();
	DrawControls();
	DrawForeground();
}

void ContainerControl::AddControl(shared_ptr<Control> control)
{
	control->SetParent(shared_from_this());
	_controls.push_back(control);
	OnControlCollectionChanged();
}

ContainerControl::~ContainerControl()
{
}

bool ContainerControl::IsLayoutDirty() const
{
	
	if(!_isLayoutDirty && _controls.size() > 0)
	{
		for(auto control : _controls)
		{
			if(control->IsLayoutDirty())
			{
				return true;
			}
		}
		return false;
	}
	return _isLayoutDirty;
}

size_t ContainerControl::GetControlsCount() const
{
	return _controls.size();
}

void ContainerControl::RemoveControl( size_t index )
{
	utils::remove(_controls, index);
	OnControlCollectionChanged();
}

void ContainerControl::OnControlCollectionChanged()
{
	Control::IsLayoutDirty(true);
}

}